import store from '../store/index.js';
import getCascaderPath from './getCascaderPath';
export default function historySearch(searchInfo, historySearch) {
  if (historySearch) {
    for (const item of searchInfo) {
      if (item.VALUE instanceof Array && item.SUBCODE) {
        const arr = [];
        for (const code of item.SUBCODE) {
          for (const key of Object.keys(historySearch)) {
            if (key === code) {
              arr.push(historySearch[key]);
              item.VALUE = arr;
            }
          }
        }
      } else if (item.VALUE instanceof Array && !item.VALUE.SUBCODE) {
        for (const key of Object.keys(historySearch)) {
          if (key === item.CODE) {
            const arr = [];
            for (const str of historySearch[key].split(',')) {
              for (const option of item.OPTIONS) {
                if (str === option.value) {
                  arr.push(str);
                }
              }
            }
            item.VALUE = arr;
            // item.VALUE = historySearch[key].split(',');
            const obj = historySearch;
            obj[key] = arr.join(',');
            store.commit('setHistorySearchInfo', obj);
          }
        }
      } else {
        for (const key of Object.keys(historySearch)) {
          if (key === item.CODE) {
            item.VALUE = historySearch[key];
            if (item.TYPE === 'CASCADER') {
              item.ARRAY = getCascaderPath(item.VALUE, item.OPTIONS);
            }
          }
        }
      }
    }
  }
}
